package com.lab.book.seata.controller;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lab.book.common.bean.User;
import com.lab.book.common.enums.SexEnum;
import com.lab.book.common.util.TBcrypt;
import com.lab.book.seata.common.bean.Address;
import com.lab.book.seata.service.AdressService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class SeataController {
	@Autowired
	private AdressService addressService;

	// @Autowired
	// private KafkaTemplate<String, Object> kafkaTemplate;

	@GetMapping("/seataTest")
	public String index() throws Exception {
		User user = new User();
		user.setName("XiaoXi");
		user.setPassword(TBcrypt.hashpw("Aa123456", TBcrypt.gensalt()));
		user.setRepwd(TBcrypt.hashpw("Aa123456", TBcrypt.gensalt()));
		user.setAge(22);
		user.setSex(SexEnum.FeMale);
		user.setEmail("666@163.com");
		user.setIdno("430381198601034136");
		user.setCreateTime(new Timestamp(new Date().getTime()));

		Address address = Address.builder().userId(user.getId()).name("岳风").mobile("95279527")
				.detail("中国深圳南山区古城").createTime(new Timestamp(new Date().getTime())).build();
		
		try {
			addressService.seataTest(user, address);
			return "ok................";
		} catch (Exception e) {
			log.info(e.getMessage());
			return "error................";
		}

	}

	@GetMapping("/send")
	public String send() throws Exception {
		testTemplateSend();
		return "ok";
	}

	public void testTemplateSend() {
		// kafkaTemplate.sendDefault("I`m send msg to default topic");
		log.info("===============111111");
		// 发送带有时间戳的消息
		// kafkaTemplate.send("topic.quick.demo1", 0, System.currentTimeMillis(), "abc",
		// "send message with timestamp");

		// 使用ProducerRecord发送消息

		// ProducerRecord record = new ProducerRecord("topic.quick.demo2", "use
		// ProducerRecord to send message");
		// kafkaTemplate.send(record);
		Address address = Address.builder().id(1L).name("岳风").mobile("1342893333").detail("中国深圳")
				.createTime(new Timestamp(new Date().getTime())).build();

		// kafkaTemplate.send("topic.quick.demo1", JSON.toJSONString(address));

	}

	/// @KafkaListener(topics = { "topic.quick.demo1", "topic.quick.demo2",
	/// "topic.quick.demo3" })
	/*
	 * public void receive(@Payload String message, @Headers MessageHeaders headers)
	 * { log.info("KafkaMessageConsumer 接收到消息：" + message);
	 * headers.keySet().forEach(key -> log.info("{}: {}", key, headers.get(key)));
	 * 
	 * JSONObject jsonObject = JSON.parseObject(message);
	 * log.info(jsonObject.getString("name")); }
	 */
}
